//
//  EditAddressViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "EditAddressViewController.h"
#import "MyhAddressPickerView.h"
#import "MapViewController.h"

@interface EditAddressViewController ()<UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *placeHolderLabel;
//@property (nonatomic, strong) UITextField *desTF;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UILabel *desLab;
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *district;
@property (nonatomic, copy) NSString *desAdd;
@property (nonatomic, assign) double latitude;
@property (nonatomic, assign) double longitude;
@end

@implementation EditAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviWith:@"更改地址"];
    [self setMainView];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)setMainView
{
    UIView *headView = [UIView new];
    if (@available(iOS 11.0, *)) {
        headView.frame = CGRectMake(0, TopBarHeight_xs, SCREENWIDTH, 68);
    } else {
        // Fallback on earlier versions
        headView.frame = CGRectMake(0, TopBarHeight, SCREENWIDTH, 68);
    }
    headView.backgroundColor = HexWithAlpha(0xF0F0F0, 0.53);
    [self.view addSubview:headView];
    
    UIImageView *leftIV = [UIImageView new];
    leftIV.frame = CGRectMake(15, 15, 14, 14);
    leftIV.image = [UIImage imageNamed:@"addSelect"];
    [headView addSubview:leftIV];
    
    UILabel *mobileLab = [UILabel new];
    mobileLab.frame = CGRectMake(leftIV.right+15, 11, SCREENWIDTH-59, 45);
    mobileLab.textColor = UIColorFromHex(0x9E9E9E);
    mobileLab.font = FontSemiboldSize(14);
    mobileLab.numberOfLines = 2;
    mobileLab.text = [NSString stringWithFormat:@"当前地址：%@",self.address];
    [headView addSubview:mobileLab];
    
    UILabel *codeLab = [UILabel new];
    codeLab.frame = CGRectMake(15, headView.bottom+30, 70, 12);
    codeLab.textColor = goldColor;
    codeLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    codeLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"详细地址"];
    [self.view addSubview:codeLab];
    
    //先创建个方便多行输入的textView
    self.textView = [UITextView new];
    self.textView.frame = CGRectMake(codeLab.left, codeLab.bottom,SCREENWIDTH-110, 45);
    self.textView.delegate = self;
    self.textView.textColor = titleColor;
    self.textView.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
    
    //再创建个可以放置默认字的lable
    self.placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,25,self.textView.width-20,20)];
    self.placeHolderLabel.text = @"点击地图获取位置";
    self.placeHolderLabel.textColor = UIColorFromHex(0xD8D8D8);
    self.placeHolderLabel.font = FontWithSize(14);
    [self.view addSubview:self.textView];
    [self.textView addSubview:self.placeHolderLabel];
    
    /*
    self.desTF = [UITextField new];
    self.desTF.frame = CGRectMake(codeLab.left, codeLab.bottom+10,SCREENWIDTH-110, 30);
    self.desTF.delegate = self;
    self.desTF.keyboardType = UIKeyboardTypeNumberPad;
    self.desTF.returnKeyType = UIReturnKeyDone;
    self.desTF.textColor = titleColor;
    self.desTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.desTF.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
    self.desTF.placeholder = @"点击地图获取位置";
    [self.desTF setValue:UIColorFromHex(0xD8D8D8) forKeyPath:@"_placeholderLabel.textColor"];
    [self.desTF setValue:FontWithSize(14) forKeyPath:@"_placeholderLabel.font"];
    [self.view addSubview:self.desTF];
    */
     
    UIButton *locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    locationBtn.frame = CGRectMake(self.textView.right+5, self.textView.top+20, 75, 25);
    [locationBtn setTitle:@"地图定位" forState:UIControlStateNormal];
    [locationBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    locationBtn.titleLabel.font = FontWithSize(16);
    [locationBtn addTarget:self action:@selector(location:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:locationBtn];
    
    UIView *MidlineView = [UIView new];
    MidlineView.frame = CGRectMake(codeLab.left, self.textView.bottom+10, SCREENWIDTH-30, 1);
    MidlineView.backgroundColor = BacklineColor;
    [self.view addSubview:MidlineView];
    
    UILabel *desLab = [UILabel new];
    desLab.frame = CGRectMake(codeLab.left, MidlineView.bottom+10, 200, 14);
    desLab.text = @"可在当前地址进行编辑";
    desLab.font = FontWithSize(14);
    desLab.textColor = UIColorFromHex(0x9E9E9E);
    desLab.hidden = YES;
    self.desLab = desLab;
    [self.view addSubview:desLab];
    
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editBtn.frame = CGRectMake(2*leftPadding, MidlineView.bottom+70, SCREENWIDTH-80, 50);
    self.editBtn.layer.cornerRadius = 25;
    self.editBtn.layer.masksToBounds = YES;
    self.editBtn.backgroundColor = goldColor;
    [self.editBtn setTitleColor:titleColor forState:UIControlStateNormal];
    //self.editBtn.backgroundColor = UIColorFromHex(0xD1D1D1);
    self.editBtn.titleLabel.font = FontWithSize(18);
   // [self.editBtn setTitleColor:UIColorFromHex(0x8D8D8D) forState:UIControlStateNormal];
    [self.editBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [self.editBtn addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.editBtn];
}

#pragma mark -- textView delegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        self.placeHolderLabel.hidden = YES;
    }
    else
    {
        self.placeHolderLabel.hidden = NO;
    }
}

- (void)location:(UIButton *)btn
{
    MapViewController *mapVC = [MapViewController new];
    mapVC.back = @"myself";
    mapVC.callBack = ^(NSString * _Nonnull location, NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull district, NSString * _Nonnull address, double latitude, double longitude)
    {
        self.desLab.hidden = NO;
        self.province = province;self.city = city;self.district = district;
        self.desAdd = location;self.latitude = latitude;self.longitude = longitude;
        self.placeHolderLabel.hidden = YES;
        self.textView.text = [NSString stringWithFormat:@"%@%@%@%@",province,city,district,location];
//        CGSize size = [TTKUtil sizeWithText:self.textView.text Height:self.textView.width Font:FontWithSize(16)];
//        self.textView.height = size.height;
    };
    [self.navigationController pushViewController:mapVC animated:YES];
}

/* 弃用
#pragma mark -- 选择q省市区
- (void)chooseCity:(UIButton *)btn
{
    MyhAddressPickerView *pickerView = [MyhAddressPickerView shareInstance];
    [pickerView showBottomView];
    [self.view addSubview:pickerView];
    
    UITextField *tf = (UITextField *)[self.view viewWithTag:55];
    pickerView.block = ^(NSString *province,NSString *city,NSString *district)
    {
        // label.text = [NSString stringWithFormat:@"%@%@市%@",province,city,district];
        tf.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:[NSString stringWithFormat:@"%@%@市%@",province,city,district]];
    };
}
*/
#pragma mark -- 修改
- (void)editClick:(UIButton *)btn
{
    if (self.textView.text.length == 0) {
        [self ShowHUDWithMessage:@"店铺地址不能为空"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.province forKey:@"province"];
    [dict setValue:self.city forKey:@"city"];
    [dict setValue:self.district forKey:@"district"];
    [dict setValue:self.desAdd forKey:@"address"];
    [dict setValue:[NSNumber numberWithDouble:self.latitude] forKey:@"latitude"];
    [dict setValue:[NSNumber numberWithDouble:self.longitude] forKey:@"longitude"];
    [dict setValue:[NSNumber numberWithInteger:self.id] forKey:@"sellerId"];
    [HttpRequest formPostPath:EditMyInfo_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
        [self ShowHUDWithMessage:@"修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
